﻿using DataInfo;
using KenLib.BusinessManager;
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web;
public partial class frm_TraineeBasicInfo : System.Web.UI.Page
{

    private bool ck = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();

        if (Session["役"] == null)
        {
            ((MasterPage)this.Master).setVisibleLogout(false);
        }
        else
        {
            ((MasterPage)this.Master).setVisibleLogout(true);
        }

        if (!IsPostBack)
        {
            txt_姓.Focus();
            //lbl_titleHeader
            //Show_就業経験有無();
            //ddl_国籍_SelectedIndexChanged(sender,e);
            bind国籍();
            //Show_就業経験有無();
            bind事業内容();


            //Error
            NReq.ErrorMessage = GetError("ER00001");
            MReq.ErrorMessage = GetError("ER00001");
            RequiredFieldValidator性別.ErrorMessage = GetError("ER00007");
            validatetxt_textbox1.ErrorMessage = GetError("ER00001");
            CustomValidatortextbox11.ErrorMessage = GetError("ER00020");
            validatetxt_出身地.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatortxt_ID番号.ErrorMessage = GetError("ER00001");
            CustomValidatorID番号.ErrorMessage = GetError("ER00004");
            CustomValidatorkiemtrangaycapCMND.ErrorMessage = GetError("ER00002");
            RequiredFieldValidatortxt_発行機関.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatortxt_宗教.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatortxt_民族.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatortxt_身長.ErrorMessage = GetError("ER00001");
            CustomValidatortxt_身長.ErrorMessage = GetError("ER00016");
            RequiredFieldValidatortxt_体重.ErrorMessage = GetError("ER00001");
            CustomValidatortxt_体重.ErrorMessage = GetError("ER00017");
            RequiredFieldValidator視力.ErrorMessage = GetError("ER00007");
            CustomValidator4.ErrorMessage = GetError("ER00001");
            CustomValidator3.ErrorMessage = GetError("ER00001");
            CustomValidator2.ErrorMessage = GetError("ER00001");
            CustomValidator5.ErrorMessage = GetError("ER00001");
            CustomValidator9.ErrorMessage = GetError("ER00001");
            CustomValidator8.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatorEmail.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatortxt_現住所.ErrorMessage = GetError("ER00001");
            RequiredFieldValidator最終学歴.ErrorMessage = GetError("ER00007");
            RequiredFieldValidator就業経験有無.ErrorMessage = GetError("ER00007");
            RequiredFieldValidator経験作業名1.ErrorMessage = GetError("ER00007");
            RequiredFieldValidatortxt_具体的仕事内容.ErrorMessage = GetError("ER00001");
            RequiredFieldValidatorkey.ErrorMessage = GetError("ER00001");
            CustomValidator経験作業名2.ErrorMessage = GetError("ER00009");
            CustomValidator経験作業名3.ErrorMessage = GetError("ER00009");
            //CustomValidatorCapcha.ErrorMessage = GetError("ER00003");
            CustomValidatorkiemtrangayTN.ErrorMessage = GetError("ER00002");
            CustomValidatortxt_在学学校名.ErrorMessage = GetError("ER00001");
            CustomValidator6.ErrorMessage = GetError("ER00001");
            RegularExpressionValidator1.ErrorMessage = GetError("ER00002");
            CustomValidator10.ErrorMessage = GetError("ER00002");
            CustomValidator11.ErrorMessage = GetError("ER00002");
            CustomValidator12.ErrorMessage = GetError("ER00002");
            CustomValidator13.ErrorMessage = GetError("ER00002");
            CustomValidator14.ErrorMessage = GetError("ER00002");
            CustomValidator15.ErrorMessage = GetError("ER00002");
            CustomValidator16.ErrorMessage = GetError("ER00002");
            CustomValidator17.ErrorMessage = GetError("ER00002");
            CustomValidator18.ErrorMessage = GetError("ER00009");
            if (Session["hobj"] != null)
            {

                基本情報 hobj = new 基本情報();
                hobj = (基本情報)Session["hobj"];
                txt_姓.Text = hobj.姓;              //ho
                txt_名.Text = hobj.名;              //ten
                ddl_性別.SelectedValue = hobj.性別 == true ? "0" : "1";//gioi tinh
                textbox1.Text = DateTime.Parse(hobj.生年月日.ToString()).ToString("yyyy/MM/dd");//ngay sinh
                ddl_国籍.SelectedValue = hobj.temp_国籍;//quoc tich
                txt_出身地.Text = hobj.出身地;      //noi sinh
                txt_ID番号.Text = hobj.ＩＤ番号;    //so cmnd
                if (!String.IsNullOrEmpty(hobj.ＩＤ番号発行日.ToString()))
                {

                    textbox2.Text = DateTime.Parse(hobj.ＩＤ番号発行日.ToString()).ToString("yyyy/MM/dd");
                }
                else
                {
                    textbox2.Text = "";
                }
                txt_発行機関.Text = hobj.発行機関;  //noi cap cmnd
                txt_宗教.Text = hobj.宗教;          //ton giao
                txt_民族.Text = hobj.民族;          //dan toc
                txt_身長.Text = hobj.身長;          //chieu cao
                txt_体重.Text = hobj.体重;          //can nang
                ddl_視力.SelectedValue = hobj.視力;
                char[] splitter = { '-' };
                if (hobj.固定電話 != null)          //so dien thoai
                {
                    if (!String.IsNullOrEmpty(hobj.固定電話))
                    {
                        string[] arrＴＥＬ = new string[3];
                        arrＴＥＬ = hobj.固定電話.Split(splitter);
                        txt_電話番号.Text = arrＴＥＬ[0];
                        txt_電話番号1.Text = arrＴＥＬ[1];
                        txt_電話番号2.Text = arrＴＥＬ[2];
                    }
                }
                if (hobj.会社携帯 != null)          //so dien thoai
                {
                    if (!String.IsNullOrEmpty(hobj.会社携帯))
                    {
                        string[] arrDD = new string[3];
                        arrDD = hobj.会社携帯.Split(splitter);
                        txt_携帯電話.Text = arrDD[0];
                        txt_携帯電話1.Text = arrDD[1];
                        txt_携帯電話2.Text = arrDD[2];
                    }
                }
                txt_Email.Text = hobj.会社メールアドレス１;//email

                if (hobj.趣味 == "-")               //so thich
                {
                    txt_趣味.Text = "";
                }
                else
                {
                    txt_趣味.Text = hobj.趣味;
                }
                txt_現住所.Text = hobj.市民権;      //dia chi
                ddl_最終学歴.SelectedValue = hobj.最終学歴;//bang cap cao nhat
                if (hobj.在学学校名 == "-")         //ten truong
                {
                    txt_在学学校名.Text = "";
                }
                else
                {
                    txt_在学学校名.Text = hobj.在学学校名;
                }
                if (hobj.卒業見込み年月 == "-")     //thoi gian tot nghiep
                {
                    textbox3.Text = "";
                }
                else
                {
                    textbox3.Text = hobj.卒業見込み年月;
                }
                string test = hobj.日本語学習経験_1.ToString();//kinh nghiem tieng nhat
                if (test == null)
                {
                    ddl_日本語学習経験.SelectedValue = "-1";
                }
                else
                {
                    ddl_日本語学習経験.SelectedValue = test;
                }
                if (hobj.就業経験有無 == true)      //kinh nghiem lam viec
                {
                    ddl_経験作業名1.SelectedValue = hobj.経験作業名1.ToString();
                    if (hobj.経験作業名2 == -1)
                    {
                        //ddl_経験作業名2.Enabled = false;
                        ddl_経験作業名3.Enabled = false;
                    }
                    ddl_経験作業名2.SelectedValue = hobj.経験作業名2.ToString();
                    ddl_経験作業名3.SelectedValue = hobj.経験作業名3.ToString();
                    ddl_就業経験有無.SelectedValue = "1";

                    dv経験作業名1.Style.Add("display", "block");
                    dv経験作業名2.Style.Add("display", "block");
                    dv経験作業名3.Style.Add("display", "block");
                }
                else
                {
                    ddl_就業経験有無.SelectedValue = "0";
                }

                txt_具体的仕事内容.Text = hobj.技能経験有無;//noi dung cong viec


            }
        }

    }
    /// <summary>
    /// Get error message
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            return error = dt.Rows[0][1].ToString();
        }
        else
        {
            return error;
        }


    }

    /// <summary>
    /// Bind data into dropdownlist(国籍)
    /// </summary>
    public void bind国籍()
    {
        try
        {
            frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
            DataTable dt = frm.get国籍();
            ddl_国籍.DataSource = dt;
            ddl_国籍.DataTextField = "国籍名_EN";
            ddl_国籍.DataValueField = "国籍名_EN";
            //ddl_国籍.SelectedIndexChanged += new EventHandler(ddl_国籍_SelectedIndexChanged);

            ddl_国籍.DataBind();
            //ListItem item1 = new ListItem("Lựa Chọn", "-1");
            ////item1.Selected = true;
            //ddl_国籍.Items.Insert(0, item1);

            ListItem selectedListItem = ddl_国籍.Items.FindByValue("Viet Nam");

            if (selectedListItem != null)
            {
                selectedListItem.Selected = true;
            }

        }
        catch { }
    }
    /// <summary>
    /// bind data into dropdownlist
    /// </summary>
    public void bind事業内容()
    {
        try
        {

            frm_HumanBasicInfoManager frm = new frm_HumanBasicInfoManager();
            DataTable dt = frm.get事業内容();
            ddl_経験作業名1.DataSource = dt;
            ddl_経験作業名1.DataTextField = "事業内容_VN";
            ddl_経験作業名1.DataValueField = "ID";
            ddl_経験作業名1.DataBind();
            ListItem item1 = new ListItem("Lựa Chọn", "-1");
            item1.Selected = true;
            ddl_経験作業名1.Items.Insert(0, item1);

            ddl_経験作業名2.DataSource = dt;
            ddl_経験作業名2.DataTextField = "事業内容_VN";
            ddl_経験作業名2.DataValueField = "ID";
            ddl_経験作業名2.DataBind();
            ListItem item2 = new ListItem("Lựa Chọn", "-1");
            item2.Selected = true;
            ddl_経験作業名2.Items.Insert(0, item2);

            ddl_経験作業名3.DataSource = dt;
            ddl_経験作業名3.DataTextField = "事業内容_VN";
            ddl_経験作業名3.DataValueField = "ID";
            ddl_経験作業名3.DataBind();
            ListItem item3 = new ListItem("Lựa Chọn", "-1");
            item3.Selected = true;
            ddl_経験作業名3.Items.Insert(0, item3);
        }
        catch { }

    }
    /// <summary>
    /// button back menu or login form
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_戻る_Click(object sender, EventArgs e)
    {
        Response.Redirect("frm_MainMenu.aspx");

        if (Session["hobj"] != null)
            Session.Remove("hobj");
    }
    //private int qt = 0;
    /// <summary>
    /// check ID番号 exist
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void CustomValidateID番号(object source, ServerValidateEventArgs args)
    {
        frm_TraineeBasicInfoManager frm = new frm_TraineeBasicInfoManager();
        DataTable dt = new DataTable();
        if (txt_ID番号.Text.Length > 0)
        {
            dt = frm.SelectData("ID番号", "応募情報", txt_ID番号.Text);
            if (dt.Rows.Count > 0)
            {
                args.IsValid = false;
                ck = false;
            }
            else
            {
                args.IsValid = true;
            }

        }

    }

    //protected void CustomValidateCapcha(object source, ServerValidateEventArgs args)
    //{
    //    CaptchaControl1.ValidateCaptcha(txt_key.Text.Trim());
    //    if (CaptchaControl1.UserValidated == false)
    //    {
    //        args.IsValid = false;
    //    }
    //    else
    //    {
    //        args.IsValid = true;
    //        btn_登録_Click(source, args);
    //    }

    //}

    /// <summary>
    /// Redirect to frm_TraineeBasicInfoCheck.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        if (CustomValidator18.IsValid)
        {
            CaptchaControl1.ValidateCaptcha(txt_key.Text.Trim());
            if (CaptchaControl1.UserValidated == false)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00003") + "');$('#ctl00_ContentPlaceHolder1_txt_key').val('');$(document).ready(function(){setTimeout(function() {$('#ctl00_ContentPlaceHolder1_txt_key').focus()}, 10)});</script>");
                txt_key.CssClass += " invalid1";
            }
            else
            {
                if (CustomValidatorID番号.IsValid == false)
                {
                    return;
                }

                基本情報 hobj = new 基本情報();

                String 電話番号;
                String 電話番号1;
                String 電話番号2;
                String 携帯電話;
                String 携帯電話1;
                String 携帯電話2;

                電話番号 = txt_電話番号.Text;
                電話番号1 = txt_電話番号1.Text;
                電話番号2 = txt_電話番号2.Text;

                携帯電話 = txt_携帯電話.Text;
                携帯電話1 = txt_携帯電話1.Text;
                携帯電話2 = txt_携帯電話2.Text;

                hobj.姓 = txt_姓.Text;                                          //họ
                hobj.名 = txt_名.Text;                                          //tên
                hobj.性別 = ddl_性別.SelectedValue.Equals("0") ? false : true;  //gioi tinh
                if (textbox1.Text.Length > 0)
                {
                    hobj.生年月日 = DateTime.Parse(textbox1.Text);              //ngay sinh
                }
                hobj.temp_国籍 = ddl_国籍.Text;                                 //quoc tich
                hobj.国籍 = ddl_国籍.SelectedIndex;
                hobj.出身地 = txt_出身地.Text;                                  //noi sinh
                hobj.ＩＤ番号 = txt_ID番号.Text;                                //CMND
                if (textbox2.Text.Length > 0)
                {
                    hobj.ＩＤ番号発行日 = DateTime.Parse(textbox2.Text);        //ngay cap CMND
                }

                hobj.発行機関 = txt_発行機関.Text;                              //noi cap CMND
                hobj.宗教 = txt_宗教.Text;                                      //ton giao
                hobj.民族 = txt_民族.Text;                                      //dan toc
                hobj.身長 = txt_身長.Text;                                      //chieu cao
                hobj.体重 = txt_体重.Text;                                      //can nang
                hobj.視力 = ddl_視力.SelectedValue;                             //thi luc
                hobj.固定電話 = 電話番号 + "-" + 電話番号1 + "-" + 電話番号2;   //dt co dinh
                hobj.会社携帯 = 携帯電話 + "-" + 携帯電話1 + "-" + 携帯電話2;   //dt di dong
                hobj.会社メールアドレス１ = txt_Email.Text;                     //email
                hobj.市民権 = txt_現住所.Text;                                  // dia chi cu tru hien tai
                if (txt_趣味.Text == "" || txt_趣味.Text == null)               //so thich
                {
                    hobj.趣味 = "-";
                }
                else
                {
                    hobj.趣味 = txt_趣味.Text;
                }
                hobj.最終学歴 = ddl_最終学歴.SelectedValue;                     //bang cap
                if (String.IsNullOrEmpty(txt_在学学校名.Text))                  // ten truong dang hoc
                {
                    hobj.在学学校名 = "-";
                }
                else
                {
                    hobj.在学学校名 = txt_在学学校名.Text;
                }
                if (String.IsNullOrEmpty(textbox3.Text))                        // ngay thang tot nghiep
                {
                    hobj.卒業見込み年月 = "-";
                }
                else
                {
                    hobj.卒業見込み年月 = textbox3.Text;
                }
                if ((ddl_日本語学習経験.SelectedValue) == "-1")                     //kinh nghiem hoc tieng nhat
                {
                    hobj.日本語学習経験_1 = null;
                }
                else
                {
                    hobj.日本語学習経験_1 = Convert.ToInt32(ddl_日本語学習経験.SelectedValue);
                }
                hobj.就業経験有無 = ddl_就業経験有無.SelectedValue.Equals("1") ? true : false; //kinh nghiem lam viec
                if (Int32.Parse(ddl_経験作業名1.SelectedValue) != -1)  //kinh nghiem lam viec 1
                {
                    hobj.経験作業名1 = Int32.Parse(ddl_経験作業名1.SelectedValue);
                }
                else
                {
                    hobj.経験作業名1 = null;
                }
                if (Int32.Parse(ddl_経験作業名1.SelectedValue) == -1)
                {
                    hobj.tem_経験作業名1 = "-";
                }
                else
                {
                    hobj.tem_経験作業名1 = ddl_経験作業名1.SelectedItem.ToString();
                }
                if (Int32.Parse(ddl_経験作業名2.SelectedValue) != -1)
                {
                    hobj.経験作業名2 = Int32.Parse(ddl_経験作業名2.SelectedValue);  //kinh nghiem lam viec 2
                }
                else
                {
                    hobj.経験作業名2 = null;
                }

                if (Int32.Parse(ddl_経験作業名2.SelectedValue) == -1)
                {
                    hobj.tem_経験作業名2 = "-";
                }
                else
                {
                    hobj.tem_経験作業名2 = ddl_経験作業名2.SelectedItem.ToString();
                }
                if (Int32.Parse(ddl_経験作業名3.SelectedValue) != -1)
                {
                    hobj.経験作業名3 = Int32.Parse(ddl_経験作業名3.SelectedValue);  //kinh nghiem lam viec 3
                }
                else
                {
                    hobj.経験作業名3 = null;
                }

                if (Int32.Parse(ddl_経験作業名3.SelectedValue) == -1)
                {
                    hobj.tem_経験作業名3 = "-";
                }
                else
                {
                    hobj.tem_経験作業名3 = ddl_経験作業名3.SelectedItem.ToString();
                }
                if (txt_具体的仕事内容.Text == "")
                {
                    hobj.技能経験有無 = "Không có";
                }
                else
                {
                    hobj.技能経験有無 = txt_具体的仕事内容.Text;                    //noi dung cong viec
                }
                hobj.家族構成人数 = "Form đăng kí không có field này.";
                hobj.在日親族有無 = true;

                Session["hobj"] = hobj;

                if (ck)
                {
                    Response.Redirect("frm_TraineeBasicInfoCheck.aspx");

                }
            }
        }
    }

    /// <summary>
    /// reset form
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        Session.Remove("hobj");
        Response.Redirect("frm_TraineeBasicInfo.aspx");

    }


    protected void ddl_国籍_SelectedIndexChanged(object sender, EventArgs e)
    {
        //qt = ddl_国籍.SelectedIndex;
        //ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + "Kinh nghiệm:" + ddl_国籍.SelectedIndex + "');</script>");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session.Remove("hobj");
        Response.Redirect("frm_TraineeBasicInfo.aspx");
    }
    protected void txt_ID番号_TextChanged(object sender, EventArgs e)
    {
        CustomValidatorID番号.Validate();
        if (CustomValidatorID番号.IsValid == false)
        {
            txt_ID番号.Focus();
        }
    }

    [WebMethod]
    public static bool IsExisted(string cmnd_no)
    {
        var returnVal = false;
        var cmnd = int.Parse(cmnd_no);
        frm_TraineeBasicInfoManager frm = new frm_TraineeBasicInfoManager();
        DataTable dt = new DataTable();
        dt = frm.SelectData("ID番号", "応募情報", cmnd_no);
        if (dt.Rows.Count > 0)
        {
            returnVal = true;
        }

        return returnVal;

    }
    protected void CustomValidator18_ServerValidate(object source, ServerValidateEventArgs args)
    {
        基本情報Manager frm = new 基本情報Manager();
        DataTable table = frm.checkEmailExists("応募情報", "", txt_Email.Text, "Email","ID");

        if (table.Rows[0][0].ToString() != "0")
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
}
